<template>
  <el-card>
    <div class="head">
      <el-button color="#1baeae" style="color: #fff" :icon="Plus" @click="add"
        >添加</el-button
      >
      <el-button
        color="#f56c6c"
        style="color: #fff"
        :icon="Delete"
        @click="dels"
        >批量删除</el-button
      >
    </div>
    <el-table @selection-change="changeStatus" :data="list" style="width: 100%">
      <el-table-column type="selection" width="55" />
      <el-table-column prop="configName" label="商品名称" />
      <el-table-column prop="" label="跳转链接">
        <template #default="scope">
          <a href="/introduce" style="text-decoration: none; color: #6bc7ff">{{
            scope.row.redirectUrl
          }}</a>
        </template>
      </el-table-column>
      <el-table-column prop="configRank" label="排序值" />
      <el-table-column prop="goodsId" label="商品编号" />
      <el-table-column prop="createTime" label="添加时间" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button text type="primary" @click="edit(scope.row)"
            >修改</el-button
          >
          <el-popconfirm
            @confirm="del([scope.row.configId])"
            title="你确定要删除吗?"
          >
            <template #reference>
              <el-button text type="primary">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        v-model:current-page="params.pageNumber"
        v-model:page-size="params.pageSize"
        :page-sizes="[10, 20, 30, 40]"
        :background="background"
        layout=" prev, pager, next"
        :total="total"
        @size-change="getList"
        @current-change="getList"
      />
    </div>
  </el-card>
  <!-- 弹窗 -->
  <el-dialog v-model="dialogFormVisible" :title="title" width="500">
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      :rules="rules"
      label-width="auto"
      class="demo-ruleForm"
      :size="formSize"
      status-icon
    >
      <el-form-item label="商品名称" prop="configName">
        <el-input v-model="ruleForm.configName" />
      </el-form-item>
      <el-form-item label="跳转链接">
        <el-input v-model="ruleForm.redirectUrl" />
      </el-form-item>
      <el-form-item label="商品编号" prop="goodsId">
        <el-input type="number" v-model="ruleForm.goodsId" />
      </el-form-item>
      <el-form-item label="排序值" prop="configRank">
        <el-input type="number" v-model="ruleForm.configRank" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from "vue";
import { api } from "../../api/api";
import { Plus, Delete } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import "element-plus/es/components/message/style/css";
const props = defineProps(["configType"]);
const background = ref<boolean>(true);
const total = ref<number>(0);
const dialogFormVisible = ref<boolean>(false);
const title = ref<string>("");
interface paramsT {
  configType: number;
  pageNumber: number;
  pageSize: number;
}
const params = reactive<paramsT>({
  configType: 4,
  pageNumber: 1,
  pageSize: 10,
});
const list = ref<any>([]);
async function getList() {
  const res: any = await api.indexConfigsApi(params);
  list.value = res.data.list;
  total.value = res.data.totalCount;
}
onMounted(() => {
  if (props.configType) params.configType = props.configType;
  getList();
});
import type { FormInstance, FormRules } from "element-plus";

interface RuleForm {
  configName: string;
  redirectUrl: string;
  goodsId: string;
  configRank: string;
  configType: number;
  configId: number;
}

const formSize = ref("default");
const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive<RuleForm>({
  configName: "",
  redirectUrl: "",
  goodsId: "",
  configRank: "",
  configType: 4,
  configId: 0,
});

const rules = reactive<FormRules<RuleForm>>({
  configName: [{ required: true, message: "请输入商品名称", trigger: "blur" }],
  redirectUrl: [{ required: true, message: "请输入跳转链接", trigger: "blur" }],
  goodsId: [{ required: true, message: "请输入商品编号", trigger: "blur" }],
  configRank: [{ required: true, message: "请输入排序值", trigger: "blur" }],
});

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      const apiUrl =
        title.value == "添加商品"
          ? api.addIndexConfigsApi
          : api.editIndexConfigsApi;
      const info = title.value == "添加商品" ? "添加成功" : "编辑成功";
      apiUrl(ruleForm).then((res: any) => {
        if (res.resultCode == 200) {
          ElMessage.success(info);
          getList();
          dialogFormVisible.value = false;
        } else {
          ElMessage.error(res.message);
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};

// 删除
async function del(ids: any[]) {
  const res: any = await api.deleteIndexConfigsApi({ ids: ids });
  if (res.resultCode == 200) {
    ElMessage.success("删除成功");
    getList();
  } else {
    ElMessage.error(res.message);
  }
}
// 批量删除勾选框
const deleteParams = ref<any[]>([]);
function changeStatus(e: any) {
  let arr: any[] = [];
  e.forEach((element: any) => {
    arr.push(element.carouselId);
  });
  deleteParams.value = arr;
}
// 批量删除
function dels() {
  del(deleteParams.value);
}

// 清空表单
function resetForm() {
  ruleForm.configName = "";
  ruleForm.configRank = "";
  ruleForm.configType = 4;
  ruleForm.redirectUrl = "";
  ruleForm.goodsId = "";
}
// 添加弹窗
function add() {
  dialogFormVisible.value = true;
  resetForm();
  title.value = "添加商品";
}
// 编辑
function edit(row: any) {
  dialogFormVisible.value = true;
  title.value = "编辑商品";
  ruleForm.configName = row.configName;
  ruleForm.configRank = row.configRank;
  ruleForm.configType = row.configType;
  ruleForm.redirectUrl = row.redirectUrl;
  ruleForm.goodsId = row.goodsId;
  ruleForm.configId = row.configId;
}
</script>

<style lang="scss">
.pagination {
  width: 100%;
  display: flex;
  margin: 20px 0;
  justify-content: center;
  align-items: center;
}
</style>
